// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import { SpriteFrame, _decorator, assetManager, resources } from "cc";
import { Config } from "../../configs/out/ConfigMgr";
import { Defer } from "../Utils/Defer";
// import { SM } from "./SoundManager";
// import { registerFont } from "fairygui-cc";

const { ccclass } = _decorator;
const configPath = "jsons/out/config";
@ccclass
export default class Res {
    private static instance: Res = null;
    public static get Ins(): Res {
        return Res.instance;
    }
    public static init() {
        this.instance = new Res();
        this.loadConfig();
    }
    private abBundles: any = {};
    private loadAssetsBundle(abName: string, endFunc: Function): void {
        assetManager.loadBundle(abName, (err, bundle) => {
            if (err !== null) {
                console.error("【RES】Load AssetBundle Error:", abName);
                this.abBundles[abName] = null;
            }
            else {
                console.log("【RES】Load AssetBundle Success:", abName);
                this.abBundles[abName] = bundle;
            }
            endFunc && endFunc();
        })
    }
    preloadDir(dir: string, tick: (t: number) => void, end: () => void) {
        resources.preloadDir(dir, (finished, total, item) => {
            console.log('【onProgress】finished:', finished, "total:", total, "item:", item);
        }, (err, data) => {
            console.log('【onComplete】err:', err, 'data:', data);
        });
    }
    async loadSprite(url: string) {
        return new Promise<SpriteFrame>((resolve, reject) => {
            resources.load(url, SpriteFrame, (err, res) => {
                if (err) {
                    console.error(err);
                    reject();
                }
                else {
                    resolve(res);
                }
            })
        })
    }
    private static async loadConfig() {
        let defer = new Defer();
        Config.init(configPath, this, () => {
            defer.resolve();
        });
        await defer.promise;
    }
}
window['Res'] = Res